﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class StackReposity : Reposity
    {
        public IEnumerable<Stack> ListStacks()
        {
            var stacks = from s in _dataContext.Stacks
                            select s;
            foreach (Stack s in stacks)
            {
                yield return s;
            }
        }

        public IEnumerable<Stack> QueryStacks(string name, int factoryId)
        {
            name = !string.IsNullOrEmpty(name) ? name : "";
            var stacks = from s in _dataContext.Stacks
                         where (
                         s.Name.Contains(name) &&
                         (factoryId > 0 ? s.FactoryId == factoryId : 1 == 1)
                         )
                         select s;
            foreach (Stack s in stacks)
            {
                yield return s;
            }
        }

        public Stack GetStackById(long id)
        {
            var stack = from s in _dataContext.Stacks
                          where id == s.Id
                          select s;

            return stack.First();
        }

        public Stack CreateStack(Stack stack)
        {
            stack.CreateTime = DateTime.Now;
            stack.ModifyTime = DateTime.Now;
            _dataContext.Stacks.InsertOnSubmit(stack);
            SubmitChanges();
            return stack;
        }

        public bool DeleteStack(Stack stack)
        {
            _dataContext.Stacks.DeleteOnSubmit(stack);
            SubmitChanges();
            return true;
        }

        public Stack EditStack(Stack stack)
        {
            stack.ModifyTime = DateTime.Now;
            SubmitChanges();
            return stack;
        }

        public IEnumerable<InitStack_FittingEquation> ListEquations(int stackId)
        {
            var equations = from r in _dataContext.InitStack_FittingEquations
                          where r.stack_id == stackId
                          orderby r.start_level ascending
                          select r;
            foreach (InitStack_FittingEquation equation in equations)
            {
                yield return equation;
            }
        }

        public InitStack_FittingEquation CreateEquation(InitStack_FittingEquation equation)
        {
            _dataContext.InitStack_FittingEquations.InsertOnSubmit(equation);
            SubmitChanges();
            return equation;
        }

        public void DeleteEquations(int stackId)
        {
            var equations = from s in _dataContext.InitStack_FittingEquations
                          where s.stack_id == stackId
                          select s;

            foreach (InitStack_FittingEquation equation in equations)
            {
                _dataContext.InitStack_FittingEquations.DeleteOnSubmit(equation);
            }
            SubmitChanges();
        }
    }
}